#ifndef MUTEX_H
#define MUTEX_H

#include <pthread.h>
#include <assert.h>

#include "Noncopyable.h"
#include "CurrentThread.h"

class Condition;

class Mutex : Noncopyable {
public:
    Mutex() {
        pthread_mutex_init(&mutex_, NULL);
    }
    ~Mutex() {
        pthread_mutex_destroy(&mutex_);
    }

    void lock() {
        pthread_mutex_lock(&mutex_);
    }
    void unlock() {
        pthread_mutex_unlock(&mutex_);
    }


private:
    pthread_mutex_t mutex_;

    friend class Condition;
};

class MutexGuard : Noncopyable {
public:
    MutexGuard(Mutex& mutex) : mutex_(mutex) {
        mutex_.lock();
    }
    ~MutexGuard() {
        mutex_.unlock();
    }
private:
    Mutex& mutex_;
};

// 防止错误用法
#define MutexGuard(x) error "MutexGuard could't be used as temp object"


#endif